<template>
  <el-card>
    <div style="margin-bottom: 16px; display: flex; gap: 12px; align-items: center;">
      <el-input v-model="search.title" placeholder="搜索标题" clearable style="width: 200px;" />
      <el-select v-model="search.status" placeholder="全部状态" clearable style="width: 140px;">
        <el-option label="全部" :value="''" />
        <el-option label="待审核" :value="0" />
        <el-option label="通过" :value="1" />
        <el-option label="驳回" :value="2" />
      </el-select>
      <el-button type="primary" @click="fetchList">查询</el-button>
      <el-button @click="onAdd">新增文章</el-button>
    </div>
    <el-table :data="list" border stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column label="封面" width="100">
        <template slot-scope="scope">
          <el-image :src="scope.row.cover" style="width: 60px; height: 40px; object-fit:cover;" :preview-src-list="[scope.row.cover]" />
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" min-width="180" />
      <el-table-column prop="username" label="作者" width="120">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatar" size="small" />
          <span style="margin-left: 6px;">{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="view_count" label="访问量" width="80" />
      <el-table-column prop="status" label="状态" width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="info">待审核</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="success">通过</el-tag>
          <el-tag v-else type="danger">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" width="160" />
      <el-table-column label="操作" width="220">
        <template slot-scope="scope">
          <el-button size="small" @click="onEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="onDelete(scope.row)">删除</el-button>
          <el-button size="small" type="warning" @click="onAudit(scope.row)">审核</el-button>
          <el-button size="small" @click="onDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :total="total"
      :page-sizes="[10, 20, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 16px; text-align: right;"
      @current-change="handlePageChange"
      @size-change="handlePageSizeChange"
    />
  </el-card>
</template>

<script>
import { getArticleList,fetchArticleList, deleteArticle, auditArticle } from '@/api/article'
import { MessageBox, Message } from 'element-ui';
export default {
  name: 'ArticleList',
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      pageSize: 10,
      search: { title: '', status: '' }
    }
  },
  mounted() {
    this.fetchList();
  },
  methods: {
    async fetchList() {
      const res = await getArticleList({ ...this.search, page: this.page, pageSize: this.pageSize });
      this.list = res.data.list.map(item => ({
        ...item,
        username: item.username || (item.user && item.user.username) || '未知',
        avatar: item.avatar || (item.user && item.user.avatar) || '',
        cover: item.cover || '',
      }));
      this.total = res.data.total;
    },
    handlePageChange(val) {
      this.page = val;
      this.fetchList();
    },
    handlePageSizeChange(val) {
      this.pageSize = val;
      this.fetchList();
    },
    onAdd() {
      this.$router.push('/article/add');
    },
    onEdit(row) {
      this.$router.push(`/article/edit/${row.id}`);
    },
    onDelete(row) {
      MessageBox.confirm('确定删除该文章吗？', '提示', { type: 'warning' })
        .then(async () => {
          await deleteArticle(row.id);
          Message.success('删除成功');
          this.fetchList();
        });
    },
    onAudit(row) {
      MessageBox.prompt('请输入审核结果（1通过 2驳回）', '审核', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[12]$/,
        inputErrorMessage: '只能输入1或2'
      }).then(async ({ value }) => {
        await auditArticle(row.id, Number(value));
        Message.success('审核成功');
        this.fetchList();
      });
    },
    onDetail(row) {
      this.$router.push(`/article/detail/${row.id}`);
    }
  }
}
</script>